package TestThread;

public class demo10 {
    public final static Object locker1 = new Object();
    public final static Object locker2 = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            synchronized (locker1){
                System.out.println("thread1线程获取到锁1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2){
                    System.out.println("thread1线程获取锁2");
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            synchronized (locker1){
                System.out.println("thread2线程获取到锁1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2){
                    System.out.println("thread2线程获取锁2");
                }
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}
